package com.csoa.community.net;

import android.app.Activity;

import com.csoa.community.activity.BaseActivity.OnUICallback;
import com.csoa.community.bean.SoapBody;
import com.csoa.community.requests.AbstractRequest;
import com.csoa.community.utils.AndroidUtils;
import com.csoa.community.utils.HttpUtils;

public class SoapAsyncTask extends BaseAsyncTask {

	public SoapAsyncTask(Activity activity, OnUICallback callback) {
		super(activity, callback);
	}

	@Override
	protected AbstractRequest doInBackground(AbstractRequest... params) {
		AbstractRequest request = (AbstractRequest) params[0];
		try {
			if (!AndroidUtils.isNetworkValidate(activity)) {
				throw new ErrorMsg("network");
			}
			if (request == null && !this.isCancelled())
				this.cancel(true);
			String url = request.getUrl();
			if (url == null && !this.isCancelled())
				this.cancel(true);
			SoapBody soap = request.getSoap();
			if (soap != null) {
				String result;
				result = HttpUtils.getInst().getStringBySoap(activity, url, soap, null);
				request.read(result);
			} else {
				publishProgress(-1, new ErrorMsg("network.soap.params"));
			}
		} catch (ErrorMsg e) {
			this.cancel(true);
			if (e.isNetWorkError()) {
				publishProgress(-1, new ErrorMsg("network"));
			} else {
				publishProgress(-1, e.toString());
			}
		}
		return request;
	}
}
